<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="车辆编号" prop="carId">
        <el-input v-model="queryParams.carId" placeholder="请输入车辆编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item label="状态" prop="whitelistStatus">
        <el-select v-model="queryParams.whitelistStatus" placeholder="请选择状态" clearable>
          <el-option v-for="dict in dict.type.park_whitelist_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item label="车牌号码" prop="licensePlate">
        <el-input v-model="queryParams.licensePlate" placeholder="请输入车牌号码" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="车主姓名" prop="customerName">
        <el-input v-model="queryParams.customerName" placeholder="请输入车主姓名" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="车名" prop="carName">
        <el-input v-model="queryParams.carName" placeholder="请输入车名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['parking:whitelist:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['parking:whitelist:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['parking:whitelist:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['parking:whitelist:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="whitelistList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="白名单编号" align="center" prop="whitelistId" /> -->
      <el-table-column label="规则编号" align="center" prop="whitelistId">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="车辆编号" align="center" prop="carId" /> -->
      <el-table-column label="车主名字" align="center" prop="customerName" />
      <el-table-column label="车牌号码" align="center" prop="licensePlate" />
      <el-table-column label="车名" align="center" prop="carName" />
      <el-table-column label="状态" align="center" prop="whitelistStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.park_whitelist_status" :value="scope.row.whitelistStatus" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['parking:whitelist:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['parking:whitelist:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改白名单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="车辆编号" prop="carId">
          <el-input v-model="form.carId" placeholder="请输入车辆编号" />
        </el-form-item> -->
        <el-form-item label="白名单编号" prop="whitelistId">
          <el-input v-model="form.whitelistId" placeholder="请输入车牌号码" />
        </el-form-item>
        <el-form-item label="状态" prop="whitelistStatus">
          <el-select v-model="form.whitelistStatus" placeholder="请选择状态">
            <el-option v-for="dict in dict.type.park_whitelist_status" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <!-- 下拉框 -->
        <el-form-item label="车牌" prop="licensePlateList">
          <el-select v-model="form.carId" placeholder="请选择车牌">
            <el-option v-for="option in licensePlateOptions" :value="option.carId" :key="option.carId" :label="option.licensePlate"
              ></el-option>
          </el-select>
        </el-form-item>
        <!-- 穿梭框 -->
        <!-- <el-transfer :titles="['车牌号', '白名单']" filterable :filter-method="filterMethod" filter-placeholder="请输入车牌号" v-model="value" :data="data">
        </el-transfer> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWhitelist, getWhitelist, delWhitelist, addWhitelist, updateWhitelist } from "@/api/parking/whitelist";
import { listCar, getCar, delCar, addCar, updateCar } from "@/api/parking/car";
export default {
  name: "Whitelist",
  dicts: ['park_whitelist_status'],
  data() {
    const generateData = _ => {
      const data = [];
      const licensePlates = [];

    };
    return {
      data: generateData(),
      value: [],
      // query用户输入的，
      filterMethod(query, item) {
        return item.label.indexOf(query) > -1;
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      //车ids
      carIds :[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 白名单表格数据
      whitelistList: [],
      // 存储选中的车牌
      selectedLicensePlate: '',
      // 存储车牌选项列表
      licensePlateOptions:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        carId: null,
        whitelistStatus: null,
        customerName: null,
        licensePlate: null,
        carName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        carId: [
          { required: true, message: "车辆编号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    fetchLicensePlates() {
      listCar(this.queryParams).then(response => {

        console.log(response,999);

        this.licensePlateOptions = response.rows

        this.data = [];
        const findCars = response.rows;
        //赋值车牌
        this.licensePlates = findCars.map(item => item.licensePlate);

        this.licensePlates.forEach((carLicensePlate, index) => {
          this.data.push({
            label: carLicensePlate,
            key: index,
          });

        });
      }).catch(error => {
          console.log("获取失败", error)
        })
    },
    /** 查询白名单列表 */
    getList() {
      this.loading = true;
      listWhitelist(this.queryParams).then(response => {
        this.whitelistList = response.rows;
        console.log(this.whitelistList,111111111111111111);
        // console.log(this.generateData.whitelistList);
        //this.cities = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        whitelistId: null,
        carId: null,
        whitelistStatus: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      //console.log(selection);
      this.ids = selection.map(item => item.whitelistId);
      this.carIds= selection.map(item => item.carId);
      //  console.log(this.ids,1111111111111);
      // console.log(this.carIds);
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加白名单";
      this.fetchLicensePlates();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const whitelistId = row.whitelistId || this.ids
      getWhitelist(whitelistId).then(response => {
        this.form = response.data;
        console.log(response.data);
        this.open = true;
        this.title = "修改白名单";
        this.fetchLicensePlates();
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.whitelistId != null) {
            updateWhitelist(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWhitelist(this.form).then(response => {
              console.log(this.value);

              this.open = false;
              this.getList();
            });
            updateCar(this.form).then(response=>{
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            })
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const whitelistIds = row.whitelistId || this.ids;
      const carIds = row.carId || this.carIds;
      this.$modal.confirm('是否确认删除白名单编号为"' + whitelistIds + '"的数据项？').then(function () {
        return delWhitelist(whitelistIds);
      }).then(() => {
        delCar(carIds);
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
      //  this.$modal.confirm('是否确认删除白名单编号为"' + whitelistIds + '"的数据项？').then(function () {
      //   return delWhitelist(whitelistIds);
      // }).then(() => {
      //   this.getList();
      //   this.$modal.msgSuccess("删除成功");
      // }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('parking/whitelist/export', {
        ...this.queryParams
      }, `whitelist_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
